#!/usr/bin/python
"""
Example test using taste
"""

__author__ = 'Andy Chu'


import os
import sys
import unittest

import taste

class _CustomVerifier(taste.StandardVerifier):

  LABELS = ['python']

  def CheckIfRunnable(self):
    if False:
      raise taste.TestPrequisiteMissing("Can't run")

  def Equal(self, left, right):
    method = self.current_method
    self.log.info('current_method: %s', method.__name__)
    self.log.info('doc: %s', method.__doc__)
    #print 'labels', method.labels
    print

    # TODO: Need log to be passed into the verifiers?  It's passed into the test
    # runner originally.
    if left == right:
      self.log.info('%s and %s are equal', left, right)
    else:
      self.log.info("%s and %s aren't equal", left, right)


class ExampleTest(taste.Test):

  # This means: Run this test with 2 different verifieres, when using
  # RunThisModule.  TODO: But it doesn't take into verifiers with arguments.
  VERIFIERS = [taste.StandardVerifier, _CustomVerifier]

  # Labels on Test classes can be used to specify groups of tests to run
  # together.
  LABELS = ['suite']

  # Labels on test methods can be used by the verifiers to give different
  # behavior (e.g. blog post format rather than some other HTML doc format.)
  @taste.labels('woo')
  @taste.no_verify('python')
  def testPassing(self):
    """A passing test."""
    self.verify.Equal(1, 1)

  @taste.only_verify('python')
  def testFailing(self):
    """A failing test."""
    self.verify.Equal(1, 0)

  def testExplicitFail(self):
    """A failing test."""
    self.verify.fail("Explicit fail")

  def testLongStrings(self):
    """A failing test."""
    self.verify.LongStringsEqual('a', 'a')
    self.verify.LongStringsEqual(
        'foo \nbar\n', 'foo\nbar', ignore_whitespace=True)

    self.verify.LongStringsEqual(
        'foo \nbar\n', 'foobar', ignore_all_whitespace=True)

    # Not equal
    self.verify.LongStringsEqual(
        'foo \nbar\n', 'fobar', ignore_all_whitespace=True)


class ExamplePyUnitTest(taste.PyUnitCompatibleTest):

  def setUp(self):
    self.log.info('Running setUp')

  def tearDown(self):
    self.log.info('Running tearDown')

  @taste.labels('foo')
  def testPassing(self):
    self.assertEqual(1, 1)

  def testFailing(self):
    self.assertEqual(1, 0)


if __name__ == '__main__':
  # This can't be done with a different verifier.  The verifiers choose at a
  # verification/assertion level.  This chooses at a module level, which is
  # faster.

  # TODO:
  #  - generalize to more versions, platforms
  #  - generalize to multiple test modules
  if len(sys.argv) > 1 and sys.argv[1] == '2.4':
    print 'Running under Python 2.4'
    taste.RunInNewInterpreter((2,4))
  else:
    taste.RunThisModule()
    print
    print '*' * 80
    print
    taste.RunTests([ExampleTest()])

